package ywpt.service.knowledge;

import java.util.ArrayList;
import java.util.List;

import org.apache.xmlbeans.impl.xb.xsdschema.RestrictionDocument.Restriction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.Department;
import ywpt.model.KnowLib;
import ywpt.model.KnowMsg;
import ywpt.model.PageInfo;
import ywpt.model.PageResultSet;
import ywpt.model.PeopleInfo;
import ywpt.model.PositionInfo;
@Service("knowMsgService")
public class KnowMsgServiceImpl extends HibernateEntityDao<KnowMsg> implements KnowMsgService{
	public KnowMsgServiceImpl(){}

	@Override
	public KnowMsg getKnowMsgById(Long id) {
		return this.get(id);
	}

	@Override
	public void saveKnowMsg(KnowMsg knowMsg, Long knowLibId, Long peopleId,
			Long departId, Long positionId) {
		if(knowLibId != null){
			knowMsg.setKnowLib(this.get(KnowLib.class, knowLibId));
		}
		if(peopleId != null){
			knowMsg.setPeople(this.get(PeopleInfo.class, peopleId));
		}
		if(departId != null){
			knowMsg.setDepart(this.get(Department.class, departId));
		}
		if(positionId != null){
			knowMsg.setPosition(this.get(PositionInfo.class, positionId));
		}
		super.save(knowMsg);
	}

	@SuppressWarnings("unchecked")
	@Override
	public PageResultSet<KnowMsg> queryByPage(int pageSize, int Page, DetachedCriteria query) {
		int totalRow = 0;
		List<KnowMsg> list = new ArrayList<KnowMsg>();
		PageResultSet<KnowMsg> pageResultSet = new PageResultSet<KnowMsg>();
		query.add(Restrictions.isNotNull("id"));
		totalRow = this.getCountByCriteria(query);		
		PageInfo pageinfo = new PageInfo(totalRow, pageSize, Page);
		//��ȡ��ҳ�ļ�¼
		query.setProjection(null);
		if(totalRow>0){
			list.addAll(this.findAllByCriteria(query, pageinfo.getPageSize(), pageinfo.getBeginIndex()));	
		}else{
			list = null;
		}
			
		
		pageResultSet.setList(list);
		pageResultSet.setPageInfo(pageinfo);
		return pageResultSet;

	}

	@Override
	public void removeMsg(Long id) {
		this.removeById(id);
	}

}
